<template>
    <div class="app-container">
        <!-- 表格 -->
        <el-table v-loading="listLoading" :data="list" 
            element-loading-text="数据加载中" border fit highlight-current-row>

            <el-table-column label="序号" width="70" align="center">
                <template slot-scope="scope">
                    {{ (page - 1) * limit + scope.$index + 1 }}
                </template>
            </el-table-column>

            <el-table-column prop="title" label="图片名称" width="80" />

            <el-table-column prop="img" label="轮播图" width="600">
                <template slot-scope="scope">
                    <img  :src="scope.row.imageUrl" alt="" style="width: 600px;height: 300px">
                </template>
            </el-table-column>

            <el-table-column prop="gmtCreate" label="添加时间" width="160"/>

            <el-table-column prop="sort" label="排序" width="60" />

            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <router-link :to="'/cms/edit/'+scope.row.id">
                        <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
                    </router-link>
                    <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination
            :current-page="page"
            :page-size="limit"
            :total="total"
            style="padding: 30px 0; text-align: center;"
            layout="total, prev, pager, next, jumper"
            @current-change="getList"
        />
    </div>
</template>

<script>
    import bannerApi from '@/api/cms/banner'

    export default {
        data() {    //定义变量和初始值
            return {
                listLoading: false,
                list: null, //查询之后接口返回集合
                page: 1,    //当前页
                limit: 5,  //每页记录数
                total: 0,   //总记录数
            }
        },
        created() { //页面渲染之前执行，一般调用methods定义的方法
            this.getList()  //调用
        },
        methods: {  
            getList(page = 1) { 
                this.page = page
                bannerApi.getBannerListPage(this.page, this.limit)
                    .then(response => { //请求成功
                        //response 接口返回的数据
                        this.list = response.data.items
                        this.total = response.data.total
                    })
            },
            removeDataById(bannerId) {  //删除数据
                this.$confirm('是否确认删除该轮播图?', '提示', {
                    confirmButtonText: '确定', 
                    cancelButtonText: '取消', 
                    type: 'warning'
                })
                .then(() => {
                    bannerApi.delBannerById(bannerId)
                        .then(response => { //删除成功
                            //提示信息
                            this.$message({ 
                                type: 'success',
                                message: '删除成功!'
                            });
                            //回到列表界面
                            this.getList()
                        })
                })
            },
            getBannerById(bannerId) {
                bannerApi.getBannerById(bannerId)
                    .then(response => {
                        thid.banner = response.data.item
                    })
            }
        }
    }
</script>